/**
 * 
 */
package com.doone.telmarket.action.admin;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.doone.telmarket.domain.SiteDirectory;
import com.doone.telmarket.domain.SiteResource;
import com.doone.telmarket.domain.factory.SiteResourceFactory;
import com.doone.telmarket.service.SiteResourceManager;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author 吴同庆
 * @date 2009-10-23
 * @description
 * @package com.doone.telmarket.action.admin
 */

@Controller
@Scope("prototype")
@ParentPackage("struts-default")
@InterceptorRefs( { @InterceptorRef(value = "fileUploadStack"), @InterceptorRef(value = "defaultStack") })
@Results( { @Result(name = "success", type = "stream",params={"contentType","text/html;charset=UTF-8","inputName","inputStream"}) })
public class CkeditorUploadAction extends ActionSupport implements ServletRequestAware {

	private static final long serialVersionUID = -2205855686599501507L;

	private static final String DEFAULT_IMAGE = "default.gif";

	private static final String DEFAULT_FLASH = "default.swf";

	Logger log = Logger.getLogger(CkeditorUploadAction.class);

	private File upload;

	@Autowired
	private SiteResourceManager siteResourceManager;

	@Autowired
	private SiteResourceFactory siteResourceFactory;
	
	private InputStream	inputStream;
	
	private HttpServletRequest httpServletRequest;

	public InputStream getInputStream() {
		return inputStream;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}
	
	public void setServletRequest(HttpServletRequest httpServletRequest) {
		this.httpServletRequest = httpServletRequest;
	}

	@Action("upload-image")
	public String uploadImage() throws Exception {
		log.debug("上传IMAGE成功：" + upload.getName());
		SiteResource siteResource = siteResourceFactory.createSiteResource(DEFAULT_IMAGE,
				new Long(new Date().getTime()).toString(), SiteDirectory.IMAGE_DIRECTORY,
				SiteDirectory.SERVICE_DIRECTORY);
		String filePath = siteResource.getFullPath();
		siteResourceManager.uploadFile(upload, filePath);
		String script = reponseScript(filePath);
		inputStream = new ByteArrayInputStream(script.getBytes("UTF-8"));
		return SUCCESS;
	}

	@Action("upload-flash")
	public String uploadFlash() throws Exception {
		log.debug("上传FLASH成功：" + upload.getName());
		SiteResource siteResource = siteResourceFactory.createSiteResource(DEFAULT_FLASH,
				new Long(new Date().getTime()).toString(), SiteDirectory.FLASH_DIRECTORY,
				SiteDirectory.SERVICE_DIRECTORY);
		String filePath = siteResource.getFullPath();
		siteResourceManager.uploadFile(upload, filePath);
		String script = reponseScript(filePath);
		inputStream = new ByteArrayInputStream(script.getBytes("UTF-8"));
		return SUCCESS;
	}

	private String reponseScript(String filePath) throws IOException {
		StringBuffer buffer = new StringBuffer();
		buffer.append("http://");
		buffer.append(httpServletRequest.getLocalAddr());
		buffer.append(":");
		buffer.append(httpServletRequest.getLocalPort());
		buffer.append(httpServletRequest.getContextPath());
		buffer.append(filePath);
		String script = "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(1, '"
				+ buffer.toString() + "');</script>";
		return script;
	}

}
